package com.abc.model;

import com.alibaba.fastjson.JSON;
import lombok.Getter;
import lombok.Setter;

import java.util.HashMap;
import java.util.Map;

@Getter
@Setter
public class Result {

    private String oper;
    private boolean succ = true;
    private String type;
    private String msg;
    private Map<String,Object> data = new HashMap<>();

    //================ 构造函数

    public Result(String oper, boolean succ, String type, String msg) {
        this.oper = oper;
        this.succ = succ;
        this.type = type;
        this.msg = msg;
    }

    public Result(String oper, boolean succ, String type,  String msg, String dataKey, String dataValue) {
        this.oper = oper;
        this.succ = succ;
        this.type = type;
        this.msg = msg;
        this.data.put(dataKey,dataValue);
    }

    //================ 静态工厂方法

    public static Result success(String oper){
        return new Result(oper,true, "success", null);
    }

    public static Result warning(String oper){
        return new Result(oper,false,"warning",null);
    }

    public static Result error(String oper){
        return new Result(oper,false,"error", null);
    }

    //================ 链式调用方法

    public Result msg(String msg){
        this.msg = msg;
        return this;
    }

    public Result type(String type){
        this.type = type;
        return this;
    }

    public Result data(String dataKey, Object dataValue){
        this.data.put(dataKey,dataValue);
        return this;
    }

    //================ 其它便捷方法

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
